package com.sicnustray.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sicnustray.common.Result;
import com.sicnustray.entity.User;
import com.sicnustray.jwt.JwtUtil;
import com.sicnustray.utils.annotation.MyLog;
import com.sicnustray.entity.Post;
import com.sicnustray.service.IPostService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yl
 * @since 2022-11-27
 */
@RestController
@RequestMapping("/post")
public class PostController {
    
        @Resource
        private IPostService postService;

        // 新增或者更新
        @MyLog(value = "新增或者更新物品")
        @PostMapping
        public Result save(@RequestBody Post post) {
                return Result.success(postService.saveOrUpdate(post));
                }

        @MyLog(value = "新增物品")
        @PostMapping("/save")
        public Result frontSave(@RequestBody Post post) {
                return Result.success(postService.save(post));
        }

        @RequiresPermissions("物品管理")
        @MyLog(value = "更新物品状态")
        @PostMapping("/save/status/{id}")
        public Result saveStatus(@PathVariable Integer id) {
                User currentUser = JwtUtil.getCurrentUser();
                Post post=postService.getById(id);
                post.setStatus(2);
                post.setClaimname(currentUser.getUsername());
                postService.updateById(post);
                return Result.success();
        }

        @RequiresPermissions("物品管理")
        @MyLog(value = "单个删除物品")
        @DeleteMapping("/{id}")
        public Result delete(@PathVariable Integer id) {
                return Result.success(postService.removeById(id));
                }

        @RequiresPermissions("物品管理")
        @MyLog(value = "批量删除物品")
        @PostMapping("/del/batch")
        public Result deleteBatch(@RequestBody List<Integer> ids) {
                return Result.success(postService.removeByIds(ids));
                }

        @MyLog(value = "查询所有物品")
        @GetMapping
        public Result findAll() {
                return Result.success(postService.list());
                }

        @MyLog(value = "查询单个物品")
        @GetMapping("/{id}")
        public Result findOne(@PathVariable Integer id) {
                return Result.success(postService.getById(id));
                }

        @MyLog(value = "分页查询物品")
        @GetMapping("/page")
        public Result findPage(@RequestParam Integer pageNum,
                               @RequestParam Integer pageSize,
                               @RequestParam(defaultValue = "")String username) {
                        QueryWrapper<Post> queryWrapper = new QueryWrapper<>();
                        if(!"".equals(username)){
                                queryWrapper.like("username",username);
                        }

                        return Result.success(postService.page(new Page<>(pageNum,pageSize),queryWrapper));
        }

        @MyLog(value = "前台分页查询物品")
        @GetMapping("/front/page")
        public Result findFrontPage(
                                @RequestParam Integer pageNum,
                                 @RequestParam Integer pageSize,
                                @RequestParam(defaultValue = "")String typename,
                               @RequestParam(defaultValue = "")String flag,
                              @RequestParam(defaultValue = "")String status) {
                QueryWrapper<Post> queryWrapper = new QueryWrapper<>();
                if(!"".equals(typename)){
                        queryWrapper.like("typename",typename);
                }
                if(!"".equals(flag)){
                        queryWrapper.like("flag",flag);
                }
                if(!"".equals(status)){
                        queryWrapper.like("status",status);
                }
                return Result.success(postService.page(new Page<>(pageNum,pageSize),queryWrapper));
                }

        }

